﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CoastalClimbing.Code;
using System.Web.Security;
using System.IO;

namespace CoastalClimbing.Auth
{
    public partial class GalleryImages : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string galleryid = Request.QueryString["GalleryID"].ToString();

                if (string.IsNullOrWhiteSpace(galleryid))
                    Response.Redirect("~/Auth/Gallery.aspx");
                else
                    this.BindInfo(galleryid);
            }
        }

        private void BindInfo(string galleryid)
        {
            if (DAL.IsVideo(galleryid))
                Response.Redirect("~/Auth/Videos.aspx");

            string galleryName = DAL.GallerySelectName(galleryid);

            lblGallery.Text = string.Concat(galleryName, " Images");

            grvImages.DataSource = DAL.GalleryImageSelect(galleryid);
            grvImages.DataBind();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrWhiteSpace(txtTitle.Text) && fupImage.HasFile)
            {
                btnAdd.Text = "Uploading...";
                btnAdd.Enabled = false;

                string galleryid = Request.QueryString["GalleryID"].ToString();

                byte[] fupBytes = fupImage.FileBytes;

                ImageObj imgObj = DAL.ImageCreate(fupImage.FileName);

                byte[] imageBytes = Common.CreateThumbnail(fupBytes, 800);

                MemoryStream ms = new MemoryStream(imageBytes);
                System.Drawing.Image img = System.Drawing.Image.FromStream(ms);
                img.Save(Server.MapPath("~/Images/" + imgObj.FilePath));

                byte[] thumbnailBytes = Common.CreateThumbnail(fupBytes, 100);
                ms = new MemoryStream(thumbnailBytes);
                img = System.Drawing.Image.FromStream(ms);
                img.Save(Server.MapPath("~/Images/thumbnail" + imgObj.FilePath));

                DAL.GalleryImageCreate(galleryid, imgObj.ID.ToString(), Membership.GetUser().ProviderUserKey.ToString(), txtTitle.Text, txtDescription.Text);

                this.BindInfo(galleryid);

                txtTitle.Text = "";
                txtDescription.Text = "";

            }

            btnAdd.Text = "Submit";
            btnAdd.Enabled = true;
        }

        protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
        {
            grvImages.EditIndex = -1;
            this.BindInfo(Request.QueryString["GalleryID"].ToString());
        }

        protected void EditRecord(object sender, GridViewEditEventArgs e)
        {
            grvImages.EditIndex = e.NewEditIndex;
            this.BindInfo(Request.QueryString["GalleryID"].ToString());
        }

        protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
        {
            string galleryid = Request.QueryString["GalleryID"].ToString();
            string imageID = grvImages.DataKeys[e.RowIndex].Value.ToString();

            GridViewRow row = grvImages.Rows[e.RowIndex];
            TextBox txtTitle = (TextBox)row.FindControl("txtTitle");
            TextBox txtDescription = (TextBox)row.FindControl("txtDescription");

            try
            {
                DAL.GalleryImageUpdate(galleryid, imageID, Membership.GetUser().ProviderUserKey.ToString(), txtTitle.Text, txtDescription.Text);
                lblMessage.Text = "Record Updated Successfully.";
            }

            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            grvImages.EditIndex = -1;
            this.BindInfo(galleryid);
        }

        protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
        {
            string galleryid = Request.QueryString["GalleryID"].ToString();
            string imageID = grvImages.DataKeys[e.RowIndex].Value.ToString();

            try
            {
                string filename = DAL.GalleryImageDelete(galleryid, imageID);
                string path = Server.MapPath("~/Images/");

                if (File.Exists(path + filename))
                    File.Delete(path + filename);
                if (File.Exists(path + "thumbnail" + filename))
                    File.Delete(path + "thumbnail" + filename);

                lblMessage.Text = "Record deleted Successfully.";
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message.ToString();
            }

            Response.Redirect(String.Format("~/Auth/GalleryImages.aspx?GalleryID={0}", galleryid));
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
                l.Attributes.Add("onclick", "javascript:return confirm('You are about to delete this image?')");
            }
        }
    }
}